package com.tt.lcd.qunyangGPRS.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.tt.lcd.qunyangGPRS.R;
import com.tt.lcd.qunyangGPRS.biz.activity.renshen.LogInfo;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class QyDaitianZhanInfo extends Activity {

	IntentFilter intf;

	ListView listView;

	private BroadcastReceiver br = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if(intent.getStringExtra("key2").equals("OK")){
				updata();
			}


		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.qy_dangtian_zhaninfo);
		;
		try {
			new LogInfo(this).send();
		} catch (ParseException e) {
			Toast.makeText(getApplicationContext(), "时间格式异常", 4).show();
		}

		intf = new IntentFilter("com.lcd.qyRec");// 参数过滤器筛选条件，可以理解为广播名称
		registerReceiver(br, intf); // 注册广播


		listView = (ListView) this.findViewById(R.id.date_list);

		updata();

	}

	//更新显示
	public void updata(){
		// 获取到集合数据

		// 创建SimpleAdapter适配器将数据绑定到item显示控件上
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		try {
//					data = new DataManager().getDateInfo();
			data = new LogInfo(this).getData();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.date_info,
				new String[] { "date", "shuliang", "muzhu" }, new int[] {
				R.id.date, R.id.shuliang, R.id.pig_shuliang1 });
		// 实现列表的显示
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new ItemClickListener()) ;
	}

	private final class ItemClickListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int position,
								long id) {

			ListView listView = (ListView) parent;
			HashMap<String, Object> data = (HashMap<String, Object>) listView
					.getItemAtPosition(position);
			String date = data.get("date").toString();

			Intent it = new Intent();
			it.setClass(QyDaitianZhanInfo.this, SiweizhanXuanze.class);

			// 下面可替换为
			// it.putExtra("str","字符串测试");
			// it.putExtra("int",30);
			Bundle bundle = new Bundle();
			bundle.putString("date", date);
			it.putExtras(bundle);

			startActivity(it);

		}

	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(br);
		super.onDestroy();
	}

}
